package com.nf.health.app.activity.global;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.utils.PreferenceHelper;
import com.nf.health.app.utils.Util;

/** 
 * @ClassName: RegisterNickActivity 
 * @Description: 注册  Name
 * @author Ian 
 * @date 2015-11-4 上午11:02:18 
 *  
 */
public class RegisterNickActivity extends BaseActivity implements OnClickListener {

	private Button next_btn;
	private String sex = "1",check_sex;
	private TextView titlebar_title_tv;
	private ImageView titlebar_back_view;
	private EditText edit_nickname;
	/**
	 * 区别从不同页面S跳转过来的显示隐藏,1表示修改部分用戶信息 0 表示引导注册信息 3表示修改整体用户信息
	 */
	private String type;
	private RadioGroup rg;
	private RadioButton radio_man,radio_woman;
	private boolean isCallBackResult; // 是否需要把结果回传给调用它的Activity  采用startActivityForResult形式
	private static final String SEX_CALL_BACK_KEY = "sex_call_back_key";
	private static final String SEX_RESULT_KEY = "sex_result";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		type = getIntent().getStringExtra("type");
		check_sex = getIntent().getStringExtra("check_sex");
		isCallBackResult = getIntent().getBooleanExtra(SEX_CALL_BACK_KEY,false);
		setTitleBar(R.layout.titlebar_base);
		setContent(R.layout.activity_register_nick);

		initView();
	}

	private void initView() {
		titlebar_title_tv = (TextView) this.findViewById(R.id.titlebar_title_tv);


		if ("1".equals(type)){
			titlebar_title_tv.setText("性别");
		}else{
			titlebar_title_tv.setText("昵称和性别");
		}


		titlebar_back_view = (ImageView) this.findViewById(R.id.titlebar_back_view);
		edit_nickname = (EditText) this.findViewById(R.id.edit_nickname);
		next_btn = (Button) this.findViewById(R.id.next_btn);
		rg = (RadioGroup) findViewById(R.id.radioGroup);
		radio_man = (RadioButton) findViewById(R.id.radio_man);
		radio_woman = (RadioButton) findViewById(R.id.radio_woman);
		rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup arg0, int arg1) {
				switch (arg1) {
				case R.id.radio_man:
					sex = "1";
					break;
				case R.id.radio_woman:
					sex = "2";
					break;
				default:
					break;
				}
			}
		});
		if ("1".equals(type)) {
			next_btn.setText("确定");
			edit_nickname.setVisibility(View.INVISIBLE);
		}else{
			titlebar_back_view.setVisibility(View.GONE);
			edit_nickname.setVisibility(View.VISIBLE);
		}

		next_btn.setOnClickListener(this);

		if("男".equals(check_sex)){
			radio_man.setChecked(true);
			radio_woman.setChecked(false);
		}else if("女".equals(check_sex)){
			radio_woman.setChecked(true);
			radio_man.setChecked(false);
		}
	}

	@Override
	public void handleActionSuccess(String actionName, Object object) {
		super.handleActionSuccess(actionName, object);
		if (actionName.equals("updataUser")) {
			showToast("修改成功！");
			finish();
			PreferenceHelper.putString(PreferenceHelper.USERSEX, sex);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.next_btn:
			if(!isCallBackResult) {
				if ("1".equals(type)) {
					provider.requestUpdateUser(null, sex, null, null, null, null, null, null, null, "updataUser");
				} else {
					String ed_name = edit_nickname.getText().toString();
					if (TextUtils.isEmpty(ed_name)) {
						showToast("请输入昵称");
						return;
					}
					if (ed_name.length() > 10) {
						showToast("名称不超过10个字");
						return;
					}
					if (!Util.isNick(ed_name)) {
						showToast(R.string.nick_prompt);
						return;
					}

					Intent intent = new Intent(RegisterNickActivity.this, RegisterHeightActivity.class);
					intent.putExtra("sex", sex);
					intent.putExtra("nick_name", edit_nickname.getText().toString());
					intent.putExtra("type", type);
					startActivity(intent);
				}
			}else{
				Intent intent = new Intent();
				intent.putExtra(SEX_RESULT_KEY,sex);
				setResult(RESULT_OK,intent);
				finish();
			}
			break;
		}
		super.onClick(v);
	}
}
